package logic

import (
	"context"
	"zhihu/models/mysql"

	"zhihu/zhihurpc/internal/svc"
	"zhihu/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AnswersListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAnswersListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AnswersListLogic {
	return &AnswersListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AnswersListLogic) AnswersList(in *zhihurpc.AnswersListRequest) (*zhihurpc.AnswersListResponse, error) {
	// todo: add your logic here and delete this line
	var answers []mysql.Answers
	mysql.DB.Debug().Preload("user").Where("problem_id", in.ProblemId).Find(&answers)

	var answerLists []*zhihurpc.AnswersList
	for _, answer := range answers {
		list := zhihurpc.AnswersList{
			Content:   answer.Content,
			HeadImage: answer.User.HeadImage,
			NickName:  answer.User.NickName,
			Id:        int64(answer.ID),
		}
		answerLists = append(answerLists, &list)
	}
	return &zhihurpc.AnswersListResponse{
		List: answerLists,
	}, nil
}
